package com.skyhub.service.impl;

import com.skyhub.common.domain.dto.HeatMap;
import com.skyhub.common.domain.pojo.*;
import com.skyhub.common.domain.vo.UserThumbsUpRecord;
import com.skyhub.mapper.HomeMapper;
import com.skyhub.service.HomeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Stream;

@Service
@RequiredArgsConstructor
public class HomeServiceImpl implements HomeService {

    private final HomeMapper homeMapper;

    private final Jedis jedis;

    @Override
    public List<Article> ArticleList() {
        List<Article>list=homeMapper.getArticleList();

        if(list.size()<5){
            return list;
        }

        Collections.shuffle(list);

        return list.subList(0,5);
    }

    @Override
    public User getUserNameByseriesId(Integer seriesId) {
        Article article = new Article();
        article.setSeriesId(seriesId);

        User user=new User();
        user.setId(homeMapper.GetUserIdBySeriesId(article));

        return (homeMapper.GetUserNameByUserId(user));
    }

    @Override
    public Article getArticleById(Integer id) {

        Article article=new Article();

        article.setId(id);

        return homeMapper.getArticleById(article);
    }

    @Override
    public List<SystemMessage> systemMessageList(Integer id){
        SystemMessage message=new SystemMessage();

        message.setUserId(id);

        List<SystemMessage>res=homeMapper.systemMessageList(message);

        return res;
    }

    @Override
    public List<Article> getArticleBySeriesId(Integer seriesId) {
        Article article=new Article();
        article.setSeriesId(seriesId);
        return homeMapper.getArticleBySeriesId(article);
    }

    @Override
    public ArticleSeries getArticleSeriesById(Integer id) {
        ArticleSeries articleSeries=new ArticleSeries();
        articleSeries.setId(id);

        ArticleSeries series = homeMapper.getArticleSeriesById(articleSeries);
        return series;
    }

    @Override
    public List<ArticleSeries> getArticleSeriesListByUserId(Integer userId) {
        ArticleSeries series=new ArticleSeries();

        series.setUserId(userId);

        List<ArticleSeries>seriesList=homeMapper.getArticleSeriesListByUserId(series);

        return seriesList;
    }

    @Override
    public void DeleteArticleById(Integer id,Integer userId) {
        Article article=new Article();

        article.setId(id);

        homeMapper.subArticleCount(userId);

        homeMapper.DeleteArticleById(article);
    }

    @Override
    public void DeleteArticleSeriesById(Integer id,Integer userId){


        ArticleSeries articleSeries=new ArticleSeries();//删除系列

        Article article=new Article();

        article.setSeriesId(id);

        articleSeries.setId(id);

        Integer count=homeMapper.GetArticleCountBySeriesId(article);//获取被删文章的数量

        homeMapper.DeleteArticleBySeriesId(article);//删除文章

        homeMapper.subArticleCountById(count,userId);

        homeMapper.DeleteArticleSeriesById(articleSeries);//删除系列
    }

    @Override
    public List<Article> GetAllArticle(Integer userId) {//获取用户所有文章
        LinkedList<Integer> list=new LinkedList<>();//数据集

        ArticleSeries articleSeries = new ArticleSeries();

        articleSeries.setUserId(userId);

        homeMapper.getArticleSeriesListByUserId(articleSeries).forEach(item->{
            list.push(item.getId());
        });

        return homeMapper.GetArticleListBySeriesId(list);
    }

    @Override
    public String parseCard(Integer userId, Integer seriesId) {//解析卡片数据

        List<ArticleColumn>columns=homeMapper.getColumns();

        List<ArticleColumn>cates=homeMapper.getCateList();

        StringBuilder result=new StringBuilder();

        ArticleSeries articleSeries = new ArticleSeries();

        articleSeries.setUserId(userId);

        homeMapper.GetArticleSeriesListByUserId(articleSeries).forEach(item->{
            if (item.getId().equals(seriesId)){
                columns.forEach(column->{
                    if(item.getColumnId().equals(column.getId())){
                        result.append(column.getName());
                    }
                });
                cates.forEach(cate->{
                    if(item.getCateId().equals(cate.getId())){
                        result.append("-").append(cate.getName());
                    }
                });
            }
        });

        return result.toString();
    }

    @Override
    public List<Article> latestArticles(Integer userId) {//获取最近发布文章数据
        LinkedList<Integer> list=new LinkedList<>();//数据集

        ArticleSeries articleSeries = new ArticleSeries();

        articleSeries.setUserId(userId);

        homeMapper.getArticleSeriesListByUserId(articleSeries).forEach(item->{
            list.push(item.getId());
        });

        List<Article>articles=homeMapper.GetArticleListBySeriesId(list);

        articles.sort((o1,o2)->{
            return o2.getCreateTime().compareTo(o1.getCreateTime());
        });


        if(articles.size()>=5){
            return articles.subList(0,5);
        }else{
            return new LinkedList<>();
        }

    }

    @Override
    public List<HeatMap> heatMap(Integer userId){//热力图数据

        HeatMap heatMap=new HeatMap();

        heatMap.setUserId(userId);

        return homeMapper.heatMapListByUserId(heatMap);
    }

    @Override
    public void addAccessCount(Integer articleId) {
        homeMapper.addAccessCount(articleId);
    }

    @Override
    public void ThumbsUpTheArticle(Integer articleId, Integer userId) {
        homeMapper.addThumbsUpCount(articleId);//添加文章表点赞数

        homeMapper.addThumbsUpRecord(userId,articleId);//添加用户文章点赞记录

        homeMapper.addThumbsUpCountFromUser(articleId);//添加用户总点赞数
    }

    @Override
    public void removeThumbsUpTheArticle(Integer articleId, Integer userId) {
        homeMapper.removeThumbsUpCount(articleId);

        homeMapper.removeThumbsUpRecord(userId,articleId);

        homeMapper.removeThumbsUpCountFromUser(articleId);
    }

    @Override
    public List<Article> getRecords(Integer userId) {

        List<Article>records=homeMapper.getRecords(userId);

        return records;
    }

    @Override
    public List<Article> hotsArticle() {
        return homeMapper.hotsArticle();
    }

}
